package com.exam.pinduoduo;

import java.util.Arrays;
import java.util.Scanner;

/**
 * date:
 * description:
 */
public class Solution004 {

    public static int res = -1;

    public static int fc = 0;
    public static int bc = 0;
    public static int lc = 0;
    public static int rc = 0;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = Integer.parseInt(scanner.nextLine());
        for (int i = 0; i < N; i++) {
            res = -1;
            fc = 0;
            bc = 0;
            lc = 0;
            rc = 0;
            String base = scanner.nextLine().trim().replace(" ", "");
            String target = scanner.nextLine().trim().replace(" ", "");
            if(base.equals(target)){
                System.out.println(0);
                continue;
            }
            dfs(base, target);
            System.out.println(res);
        }
    }

    public static void dfs(String base, String target){
        if(fc > 2 || bc > 2 || lc > 2 || rc > 2){
            return;
        }
        if(base.equals(target)){
            res = res == -1 ? fc + bc+ lc + rc : Math.min(res, fc + bc+ lc + rc);
            return;
        }
        char[] b = base.toCharArray();
        fc++;
        String front = new String(new char[]{b[5], b[4], b[2], b[3], b[0], b[1]});
        dfs(front, target);
        fc--;
        bc++;
        String back = new String(new char[]{b[4], b[5], b[2], b[3], b[1], b[0]});
        dfs(back, target);
        bc--;
        lc++;
        String left = new String(new char[]{b[3], b[2], b[0], b[1], b[4], b[5]});
        dfs(left, target);
        lc--;
        rc++;
        String right = new String(new char[]{b[2], b[3], b[1], b[0], b[4], b[5]});
        dfs(right, target);
        rc--;
    }
}
